package com.zhang.teachingcommunity_spring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.teachingcommunity_spring.mapper.CommentMapper;
import com.zhang.teachingcommunity_spring.mapper.UserMapper;
import com.zhang.teachingcommunity_spring.mapper.entity.Comment;
import com.zhang.teachingcommunity_spring.mapper.entity.Post;
import com.zhang.teachingcommunity_spring.mapper.entity.User;
import com.zhang.teachingcommunity_spring.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class commentController {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/getCommentList")
    private Result getCommentList(Integer id) {
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("post_id",id);
        List<Comment> comments = commentMapper.selectList(commentQueryWrapper);
       if(comments .size() ==0) {
           return Result.success("获取成功",null);
       }
        QueryWrapper<Comment> commentQueryWrapper2 = new QueryWrapper<>();
        commentQueryWrapper2.eq("post_id",id).isNull("pid").isNull("origin_Id");
        List<Comment>  fatherList = commentMapper.selectList(commentQueryWrapper2);
        fatherList.forEach(item -> {
            Integer commentId = item.getCommentId();
            Integer userId = item.getUserId();
            User user = userMapper.selectById(userId);
            if(user!=null) {
                item.setUser(user);
                QueryWrapper<Comment> commentQueryWrapper3= new QueryWrapper<>();
                commentQueryWrapper3.eq("origin_Id", commentId);
                List<Comment> comments1 = commentMapper.selectList(commentQueryWrapper3);
                comments1.forEach(sonItem -> {
                    Integer pid = sonItem.getPid();
                    Integer userId1 = sonItem.getUserId();
                    if(pid !=null ) {
                        User replyUser = userMapper.selectById(pid);
                        if(replyUser!=null) {
                            sonItem.setReplyUser(replyUser);
                        }
                    }
                    if(userId1 !=null ) {
                        User user1 = userMapper.selectById(userId1);
                        if(user1!=null) {
                            sonItem.setUser(user1);
                        }
                    }
                });
                item.setChildrenList(comments1);
            }

        });
        return Result.success("获取成功",fatherList);
    }
    @PostMapping("/insert")
    private Result insert(@RequestBody Comment comment){
        System.out.println(comment);
        int insert = commentMapper.insert(comment);
        if(insert == 1) {
            return Result.success("评论成功",true);
        }
       return Result.success("评论失败",false);
    }
}
